import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import iView from 'iview'
import menu from '@/models/setting/sys/menu'
import Layout from '@/components/containers/Layout.vue'

Vue.use(Router)
const router = new Router({
  routes: [{
    path: '/',
    name: 'index',
    redirect: '/setting/sys',
    component: Layout,
    children: [{
      path: 'setting',
      redirect: '/setting/sys',
      name: 'setting',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'sys',
        redirect: '/setting/sys/user',
        name: 'setting-sys',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'user',
          name: 'setting-sys-user',
          component: () => import('@/views/setting/sys/user/index.vue'),
          meta: {}
        }, {
          path: 'menu',
          name: 'setting-sys-menu',
          component: () => import('@/views/setting/sys/menu/index.vue'),
          meta: {}
        }, {
          path: 'role',
          name: 'setting-sys-role',
          component: () => import('@/views/setting/sys/role/index.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'member',
      redirect: '/member/sign',
      name: 'member',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'sign',
        redirect: '/member/sign/index',
        name: 'member-sign',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'member-sign-index',
          component: () => import('@/views/member/sign/index.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'me',
      redirect: '/me/center',
      name: 'me',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'center',
        redirect: '/me/center/index',
        name: 'me-center',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'me-center-index',
          component: () => import('@/views/me/index.vue'),
          meta: {}
        }, {
          path: 'password',
          name: 'me-center-password',
          component: () => import('@/views/me/password.vue'),
          meta: {}
        }, {
          path: 'profile',
          name: 'me-center-profile',
          component: () => import('@/views/me/profile.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'serverplatform',
      redirect: '/serverplatform/task',
      name: 'serverplatform',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'task',
        redirect: '/serverplatform/task/type',
        name: 'task',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'type',
          name: 'server-task-index',
          component: () => import('@/views/serverplatform/task/index.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'car',
      redirect: '/car/servertype',
      name: 'car',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'servertype',
        redirect: '/car/servertype/index',
        name: 'servertype',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'car-servertype-index',
          component: () => import('@/views/car/servertype/index.vue'),
          meta: {}
        }]
      }, {
        path: 'role',
        redirect: '/car/role/index',
        name: 'role',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'car-role-index',
          component: () => import('@/views/car/role/index.vue'),
          meta: {}
        }]
      }, {
        path: 'query',
        redirect: '/car/query/index',
        name: 'query',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'car-query-index',
          component: () => import('@/views/car/query/index.vue'),
          meta: {}
        }, {
          path: 'list',
          name: 'car-query-list',
          component: () => import('@/views/car/query/list.vue'),
          meta: {}
        }]
      }, {
        path: 'lottery',
        redirect: '/car/lottery/index',
        name: 'lottery',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'car-lottery-index',
          component: () => import('@/views/car/lottery/index.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'member',
      redirect: '/member/role',
      name: 'member-role',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'role',
        redirect: '/member/role/index',
        name: 'role-index',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'member-role-index',
          component: () => import('@/views/member/role/index/index.vue'),
          meta: {}
        }, {
          path: 'list',
          name: 'member-list-index',
          component: () => import('@/views/member/role/list/index.vue'),
          meta: {}
        }]
      }, {
        path: 'recomcode',
        redirect: '/member/recomcode/list',
        name: 'recomcode-index',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'list',
          name: 'member-recomcode-list',
          component: () => import('@/views/member/recomcode/index.vue'),
          meta: {}
        }]
      }]
    }, {
      path: 'mobilecontrol',
      redirect: '/mobilecontrol/activity',
      name: 'mobilecontrol',
      component: {
        render(c){
          return c('router-view')
        }
      },
      children: [{
        path: 'activity',
        redirect: '/mobilecontrol/activity/index',
        name: 'activity',
        component: {
          render(c){
            return c('router-view')
          }
        },
        children: [{
          path: 'index',
          name: 'mobilecontrol-activity-index',
          component: () => import('@/views/mobilecontrol/activity/index.vue'),
          meta: {}
        }, {
          path: 'add',
          name: 'mobilecontrol-activity-add',
          component: () => import('@/views/mobilecontrol/activity/add.vue'),
          meta: {}
        }, {
          path: 'edit',
          name: 'mobilecontrol-activity-edit',
          component: () => import('@/views/mobilecontrol/activity/edit.vue'),
          meta: {}
        }]
      }]
    }]
  }, {
    path: '/sign/in',
    component: () => import('@/views/sign/in.vue'),
    meta: {
      title: '登录系统',
      mainMenu: '/sign'
    }
  }]
})
router.beforeEach(async (to, from, next) =>{
  try{
    if(await store.dispatch('isSign')){
      if(to.path === '/sign/in'){
        next({path: '/'})
      }else{
        if(store.state.menus.length === 0){
          const ret = await menu.getUserMenu()
          if(ret.code === 200){
            ret.data.push(userMenus)
            store.commit('SET_MENUS', ret.data)
          }
        }
        store.commit('SET_CURRENT_MAIN_MENU', to.matched[1].path)
        iView.LoadingBar.start()
        next()
      }
    }else{
      if(to.path === '/sign/in'){
        next()
      }else{
        next({path: '/sign/in'})
      }
    }
  }catch(err){
    console.log(err)
    // await store.dispatch('signOut')
    // if (to.path === '/sign/in') {
    //   next()
    // } else {
    //   next({ path: '/sign/in' })
    // }
  }
})
router.afterEach((to, from) =>{
  iView.LoadingBar.finish()
})
const userMenus = {
  name: '',
  url: '/me',
  icon: 'person',
  submenus: [{
    name: '个人中心',
    url: '/me/center',
    submenus: [{
      name: '我的资料',
      url: '/me/center/index',
      icon: 'icon-puzzle'
    }, {
      name: '修改密码',
      url: '/me/center/password',
      icon: 'icon-puzzle'
    }, {
      name: '修改资料',
      url: '/me/center/profile',
      icon: 'icon-puzzle'
    }]
  }]
}
export default router
